package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.RechargeOrderInfoMapper;
import com.cdks.wuxiu.entity.mysql.RechargeOrderInfo;

import java.util.List;

import org.springframework.stereotype.Service;


/**
 * 订单信息表 服务实现类
 */
@Service
public class RechargeOrderInfoServiceImpl extends ServiceImpl<RechargeOrderInfoMapper, RechargeOrderInfo> implements RechargeOrderInfoService {

    @Override
    public RechargeOrderInfo getByOrderNo(String orderNo) {
        QueryWrapper<RechargeOrderInfo> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(RechargeOrderInfo::getOrderNo, orderNo);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public RechargeOrderInfo getByThirdNo(String thirdNo) {
        QueryWrapper<RechargeOrderInfo> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(RechargeOrderInfo::getThirdNo, thirdNo);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public RechargeOrderInfo getByPriceIdWithStatus(String priceId, int status) {
        QueryWrapper<RechargeOrderInfo> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(RechargeOrderInfo::getPriceId, priceId)
                        .eq(RechargeOrderInfo::getStatus, status);

        return baseMapper.selectOne(wrapper.last("LIMIT 1"));
    }

    @Override
    public RechargeOrderInfo getByOrderNoWithStatus(String orderNo, int status) {
        QueryWrapper<RechargeOrderInfo> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(RechargeOrderInfo::getOrderNo, orderNo)
                        .eq(RechargeOrderInfo::getStatus, status);

        return baseMapper.selectOne(wrapper.last("LIMIT 1"));
    }

    @Override
    public int updateByStatus(RechargeOrderInfo order, int status) {
        UpdateWrapper<RechargeOrderInfo> wrapper = new UpdateWrapper<>();

        wrapper.lambda().eq(RechargeOrderInfo::getOrderNo, order.getOrderNo())
                        .eq(RechargeOrderInfo::getStatus, status);

        int ret  = baseMapper.update(order, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public List<RechargeOrderInfo> getRecord(long userId, int page, long start, long stop) {
        QueryWrapper<RechargeOrderInfo> wrapper  = new QueryWrapper<>();
        
        wrapper.lambda().between(RechargeOrderInfo::getCreateTime, start, stop)
                        .eq(RechargeOrderInfo::getUserId, userId)
                        .eq(RechargeOrderInfo::getStatus, Constant.PAY_STATUS_PAYED)
                        .orderByDesc(RechargeOrderInfo::getId)
                        .select(RechargeOrderInfo::getCreateTime,
                                RechargeOrderInfo::getOrderNo,
                                RechargeOrderInfo::getMoney,
                                RechargeOrderInfo::getBean,
                                RechargeOrderInfo::getType,
                                RechargeOrderInfo::getStatus);
        wrapper.last("LIMIT " + page * Constant.LIST_PAGE_SIZE + "," + Constant.LIST_PAGE_SIZE);
        
        return baseMapper.selectList(wrapper);
    }
}
